package com.example.common.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constant.MessageConstant;
import com.example.common.entity.mq.MqMessageEntity;
import com.example.common.web.dao.MqMessageDao;
import com.example.common.web.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *     mq消息持久化实现类
 * </p>
 *
 * @author zyred
 * @since v 0.1
 **/
@Slf4j
@Service("mqMessageServiceImpl")
public class MqMessageServiceImpl extends ServiceImpl<MqMessageDao, MqMessageEntity>
        implements MqMessageService {


    @Override
    public void insertMessage(String id, String exchange, String routingKey, String className, Object message) {
        log.info(">>>>>>>> message service insert <<<<<<<<");
        MqMessageEntity entity = new MqMessageEntity()
                .setMessageId(id)
                .setToExchange(exchange)
                .setRoutingKey(routingKey)
                .setCreateTime(LocalDate.now())
                .setUpdateTime(LocalDate.now())
                .setClassType(className)
                .setContent(JSONObject.toJSONString(message))
                .setMessageStatus(MessageConstant.MessageStatus.CREATE_NEW.getCode());

        log.info("message entity :{} ", entity);
        this.baseMapper.insert(entity);
    }

    @Override
    public void updateMessage(int status, String reason, String id) {
        log.info(">>>>>>>> message service update <<<<<<<<");
        log.info("message id :{}, status: {}", id, status);
        this.baseMapper.update(null,
                Wrappers.<MqMessageEntity>lambdaUpdate()
                        .set(MqMessageEntity::getMessageStatus, status)
                        .set(MqMessageEntity::getReason, reason)
                        .eq(MqMessageEntity::getMessageId, id)
        );
    }
}
